package com.example.service.impl;

import com.example.dao.QishouDao;
import com.example.entity.OrderQs;
import com.example.dao.OrderQsDao;
import com.example.service.OrderQsService;
import com.example.util.ResponseCode;
import com.example.util.ResponseData;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (OrderQs)表服务实现类
 *
 * @author makejava
 * @since 2023-06-27 22:48:44
 */
@Service("orderQsService")
public class OrderQsServiceImpl implements OrderQsService {
    @Resource
    private OrderQsDao orderQsDao;

    @Resource
    private QishouDao qishouDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderQs queryById(Integer id) {
        return this.orderQsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param orderQs     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<OrderQs> queryByPage(OrderQs orderQs, PageRequest pageRequest) {
        long total = this.orderQsDao.count(orderQs);
        return new PageImpl<>(this.orderQsDao.queryAllByLimit(orderQs, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param orderQs 实例对象
     * @return 实例对象
     */
    @Override
    public OrderQs insert(OrderQs orderQs) {
        this.orderQsDao.insert(orderQs);
        return orderQs;
    }

    /**
     * 修改数据
     *
     * @param orderQs 实例对象
     * @return 实例对象
     */
    @Override
    public OrderQs update(OrderQs orderQs) {
        this.orderQsDao.update(orderQs);
        return this.queryById(orderQs.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.orderQsDao.deleteById(id) > 0;
    }

    @Override
    public ResponseData insertOrderQs(String orderId, String token) {
        String qishouId = qishouDao.queryqishouidByToken(token);
        return new ResponseData(ResponseCode.SUCCESS01, orderQsDao.insertOrderQs1(orderId,qishouId));
    }



    @Override
    public ResponseData queryAllOrderQs() {
        return new ResponseData(ResponseCode.SUCCESS01, orderQsDao.queryAllOrderQs());
    }

    @Override
    public ResponseData queryOrderQsByState(String status, String token) {
        String qishouId = qishouDao.queryqishouidByToken(token);
        List<OrderQs> orderQs = orderQsDao.queryOrderQsByStateAndQishouId(status, qishouId);
        return new ResponseData(ResponseCode.SUCCESS01, orderQs);
    }

    @Override
    public ResponseData updStatus(String orderId) {
        return new ResponseData(orderQsDao.updStatus(orderId));
    }
}
